package leetcode.greedy;

import java.util.Arrays;

public class LargestNumber {

    public static void main(String[] args) {
        int[] data = {1, 43, 4, 30, 50, 31, 3, 5};
        System.out.println(largestNumber(data));
    }

    // 排序中的贪心
    public static String largestNumber(int[] nums) {
        int n = nums.length;
        String[] ss = new String[n];
        for (int i = 0; i < n; i++) {
            ss[i] = "" + nums[i];
        }
        StringBuilder stringBuffer = new StringBuilder();
        Arrays.sort(ss, (a, b) -> {
            String sa = a + b, sb = b + a;
            return sb.compareTo(sa);
        });
        for (String s : ss) {
            stringBuffer.append(s);
        }
        // 特判，全是0的情况
        int index = 0;
        while (index < stringBuffer.length() - 1 && stringBuffer.charAt(index) == '0') {
            index++;
        }
        return stringBuffer.substring(index);

    }
}
